package com.banzhi.shop.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.ViewPagerActivity;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.GroupingDetailsBean;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.BitmapUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.view.SpacingTextView;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2018/7/24.
 */

public class GroupSucaiAdapter extends BaseAdapter<GroupingDetailsBean> {
    private Context context;
    public GroupSucaiAdapter(Context context, List<GroupingDetailsBean> data) {
        super(context, data);
        this.context = context;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final GroupingDetailsBean data = getData().get(position);
        viewHolder holder = null;
        LinearLayoutManager layoutManager = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = getInflater().inflate(R.layout.item_sucai2, null);
            holder.tvtitle = (SpacingTextView) convertView.findViewById(R.id.tv_title);
            holder.tvtitle.setLetterSpacing(1);
            holder.rvimage = (RecyclerView) convertView.findViewById(R.id.rv_image);
            holder.llbaochun = (LinearLayout) convertView.findViewById(R.id.ll_baocun);
            holder.llcopy = (LinearLayout) convertView.findViewById(R.id.ll_copy);

            layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            holder.rvimage.setLayoutManager(layoutManager);
            convertView.setTag(holder);
            holder.tvtitle.setText(data.getShare_desc());

            CommonAdapter<String> adapter = new CommonAdapter<String>(context, R.layout.item_sucai_recycleview, data.getThumb_url()) {
                @Override
                public void convert(ViewHolder holder, String s, int position) {
                    holder.setImageURI(R.id.iv_image, s);
                }
            };
            holder.rvimage.setAdapter(adapter);
            adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(getContext(), ViewPagerActivity.class);
                    intent.putExtra("images", (Serializable) data.getThumb_url());
                    intent.putExtra("position", position);
                    getContext().startActivity(intent);

                }
            });
            holder.llbaochun.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //保存图片
                    SavePictures();
                }

                private void SavePictures() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < data.getThumb_url().size(); i++) {
                                BitmapUtils.saveImageToGallery((Activity) getContext(), BitmapUtils.returnBitmap(data.getThumb_url().get(i)));
                            }
                        }
                    }).start();
                    int sum = data.getThumb_url().size();
                    ToastUtils.makeToastLong("保存成功" + sum + "张");
                }
            });
            holder.llcopy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ShareUtils.copyLianjie((Activity) getContext(),data.getShare_desc());
                }
            });
        }
        return convertView;

    }
    class viewHolder {
        SpacingTextView tvtitle;
        RecyclerView rvimage;
        LinearLayout llbaochun;
        LinearLayout llcopy;

    }
}
